rm(list = setdiff(ls(), lsf.str()))

#load data
data<-haven::read_sav("Study 1/Original Data/DA3145.sav")

#sex
data$female<-ifelse(data$P47==2,1,0)

#age
data$age<-data$P48

#income
data$income<- car::recode(data$P64, "99=NA; 98=NA")
data$income<-zero1(data$income)
data$income[is.na(data$income)==TRUE]=2

#income missing
data$income_missing<-ifelse(data$income==2,1,0)

#Extraversion
data$E_reserved<- car::recode(data$P6701, "8=NA; 9=NA")
data$E_extravert<- 6-(car::recode(data$P6706, "8=NA; 9=NA"))
data$ext<-rowMeans(data.frame(data$E_extravert, data$E_reserved), na.rm=T)

#Openness
data$O_artistic<-6-car::recode(data$P6705, "8=NA; 9=NA")
data$O_imagination<-car::recode(data$P6710, "8=NA; 9=NA")
data$open<-rowMeans(data.frame(data$O_artistic, data$O_imagination), na.rm=T)

#Agreeableness
data$A_empathy<-6-car::recode(data$P6704, "8=NA; 9=NA")
data$A_critical<-car::recode(data$P6707, "8=NA; 9=NA")
data$agre<-rowMeans(data.frame(data$A_critical, data$A_empathy), na.rm=T)

#Neurotcisim
data$N_stressed<-6-car::recode(data$P6703, "8=NA; 9=NA")
data$N_nervous<-6-car::recode(data$P6709, "8=NA; 9=NA")
data$neu<-rowMeans(data.frame(data$N_stressed, data$N_nervous), na.rm=T)

#Conscientiousness
data$C_lazy<-car::recode(data$P6702, "8=NA; 9=NA")
data$C_getthingsdone<-6-car::recode(data$P6708, "8=NA; 9=NA")
data$con<-rowMeans(data.frame(data$C_lazy, data$C_getthingsdone), na.rm=T)

#Vote Podemos
data$populist_vote<-ifelse(data$P31==3, 1,  ifelse(data$P31==77 | data$P31==96 | data$P31==99, NA, 0))

#Multinomial
data$populist_multinom<-ifelse(data$P31==3, 1,
                               ifelse(data$P31==1 | data$P31==2, 2,
                                      ifelse(data$P31> 3 & data$P31<77, 3,
                                             ifelse(data$P31> 76 & data$P31<100, 4, NA))))
data$populist_multinom<-as.factor(data$populist_multinom)
levels(data$populist_multinom) <- c("Podemos","Mainstream","Opposition", "Didn't")
data$populist_multinom <- relevel(data$populist_multinom, ref = "Podemos")

#vote
data$turnout<-ifelse(data$P29==5, 0, 1)

#Populist, oither, did not vote
data$populist_other_not<- data$populist_vote
data$populist_other_not[data$turnout==1]=2

#Left-right
data$lr_placement<-car::recode(data$P35, "98=NA; 99=NA")

#cynicism: Politicians do not care much about what they think. people like you
data$cynicism1<-car::recode(data$P301, "4=1; 3=2; 2=3; 1=4; 8=NA; 9=NA")
data$cynicism2<-car::recode(data$P303, "4=1; 3=2; 2=3; 1=4; 8=NA; 9=NA")
data$cynicism<-rowMeans(data.frame(data$cynicism1, data$cynicism2), na.rm=T)

#Education: ISCED categories
data$education<- ifelse(data$P54<3, 0, data$P54A)
data$education<-car::recode(data$education, "0=1; 2=1; 3=1; 4=1; 5=2; 6=3; 7=2; 8=2; 9=3; 10=4; 11=3; 12=3; 13=3; 14=3; 15=3; 16=3; 17=3; 18=3; 19=3; 20=3; 21=5; 22=5; 23=5; 24=5; 25=5; 26=5; 27=6; 99=6")

data$edu1<-ifelse(data$education==1,1,0)
data$edu2<-ifelse(data$education==2,1,0)
data$edu3<-ifelse(data$education==3,1,0)
data$edu4<-ifelse(data$education==4,1,0)
data$edu5<-ifelse(data$education==5,1,0)
data$edu6<-ifelse(data$education==6,1,0)

save(data, file="Study 1/Altered Data/Study1_Spain.Rdata")

